package com.qipay.mapper;

import com.qipay.baas.model.Message;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;
@Mapper
public interface MessageMapper extends com.qipay.baas.mapper.MessageMapper {
    @Select({
            "select",
            "id, send_by, send_name, receive_by, receive_name, content, `status`, `type`, ",
            "create_time",
            "from message",
            "where receive_by = #{uid,jdbcType=INTEGER} and status=0 order by id desc"
    })
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "send_by", property = "sendBy", jdbcType = JdbcType.INTEGER),
            @Result(column = "send_name", property = "sendName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "receive_by", property = "receiveBy", jdbcType = JdbcType.INTEGER),
            @Result(column = "receive_name", property = "receiveName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "content", property = "content", jdbcType = JdbcType.VARCHAR),
            @Result(column = "status", property = "status", jdbcType = JdbcType.TINYINT),
            @Result(column = "type", property = "type", jdbcType = JdbcType.TINYINT),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.TIMESTAMP)
    })
    List<Message> selectByReceiveIdUnread(int uid);

    @Select({
            "select",
            "id, send_by, send_name, receive_by, receive_name, content, `status`, `type`, ",
            "create_time",
            "from message",
            "where receive_by = #{uid,jdbcType=INTEGER} order by id desc limit #{offset}, #{size}"
    })
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "send_by", property = "sendBy", jdbcType = JdbcType.INTEGER),
            @Result(column = "send_name", property = "sendName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "receive_by", property = "receiveBy", jdbcType = JdbcType.INTEGER),
            @Result(column = "receive_name", property = "receiveName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "content", property = "content", jdbcType = JdbcType.VARCHAR),
            @Result(column = "status", property = "status", jdbcType = JdbcType.TINYINT),
            @Result(column = "type", property = "type", jdbcType = JdbcType.TINYINT),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.TIMESTAMP)
    })
    List<Message> selectByReceiveId(@Param("uid")int uid, @Param("offset") int offset, @Param("size") int size);

    @Select({"select count(id) from `message`",
            "where receive_by = #{uid,jdbcType=INTEGER}"})
    long countByReceiveId(int uid);

    @Update({
            "update message",
            "set `status` = 1",
            "where receive_by = #{uid,jdbcType=INTEGER} and status=0"
    })
    int updateStatusByReceiveId(int uid);
}
